package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.services.infrastructure.bean.SubcompanyBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.SubcompanyMapper;
import com.molichuxing.services.infrastructure.dto.request.create.SubcompanyCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.SubcompanyModifyDto;
import com.molichuxing.services.infrastructure.dto.response.SubcompanyDto;
import com.molichuxing.services.infrastructure.service.SubcompanyService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 分子公司 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-13
 */
@Service("subcompanyService")
public class SubcompanyServiceImpl  implements SubcompanyService {

  @Resource
  private SubcompanyMapper subcompanyMapper;

  @Override
  public SubcompanyDto getSubcompanyNameById(Integer id) {
    //根据分子公司id 获取分子公司名称
    SubcompanyBean subcompanyBean = subcompanyMapper.getById(id);
    return Convert.toSubcompanyBeanDto(subcompanyBean);
  }

  @Override
  public Map<Integer, SubcompanyDto> getSubcompanyNameByIds(List<Integer> ids) {
    List<SubcompanyBean> list = subcompanyMapper.getSubcompanyByIds(ids);
    if(null == list || list.size() == 0) {
      return null;
    }
    return Convert.toSubcompanyDto(list).stream().collect(Collectors.toMap(SubcompanyDto::getId, a -> a,(k1, k2)->k1));
  }

  @Override
  public List<SubcompanyDto> getSubcompanyByCityId(List<Integer> cityIds) {
    return Convert.toSubcompanyDtoList(subcompanyMapper.getSubcompanyByCityId(cityIds));
  }

  @Override
  public Paged<SubcompanyDto> getSubcompanyListPage(String name, String responsibleAccountName,
                                                    Integer regionId, Integer pageNum, Integer pageSize) {

    int count = subcompanyMapper.getSubcompanyPageCount(name,responsibleAccountName,regionId);
    if(count <= 0) {
      return new Paged<>();
    }
    List<SubcompanyBean> pageResult = subcompanyMapper.getSubcompanyPage(name, responsibleAccountName, regionId, pageNum, pageSize);
    return new Paged<>(Convert.toSubcompanyPageBeanDto(pageResult), count, pageSize );

  }


  @Override
  public int createSubcompany(SubcompanyCreateDto subcompanyCreateDto) {
    SubcompanyBean subcompanyBean = new SubcompanyBean();
    BeanUtils.copyProperties(subcompanyCreateDto, subcompanyBean);
    subcompanyBean.setStatus(1);
    //false == 0   true ==1
    subcompanyBean.setIsDelete(false);
    subcompanyBean.setGmtCreate(LocalDateTime.now());
    return subcompanyMapper.createSubcompanyBean(subcompanyBean);

  }

  @Override
  public int modifySubcompany(SubcompanyModifyDto modifyDto) {
    SubcompanyBean subcompanyBean = new SubcompanyBean();
    BeanUtils.copyProperties(modifyDto, subcompanyBean);
    subcompanyBean.setId(modifyDto.getId());
    return subcompanyMapper.updateSubcompanyBean(subcompanyBean);
  }

  @Override
  public List<SubcompanyDto> getSubcompanyByName(String name) {
    List<SubcompanyDto> subcompanyDtoList  = new ArrayList<>();
    if(name ==null){
    return  subcompanyDtoList;
    }
    subcompanyDtoList = Convert.toSubcompanyDtoList(subcompanyMapper.getSubcompanyByName(name));
    return  subcompanyDtoList;
  }

  @Override
  public SubcompanyDto getSubcompanyByFullName(String fullName) {
    return  Convert.toSubcompanyDto(subcompanyMapper.getSubcompanyByFullName(fullName));
  }
}
